package cn.bingoo.handy;

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import cn.bingoo.swing.utils.Swings;

import com.mycila.maven.ui.MavenDeployer;

public class MainFrame extends JFrame {
    private static final long serialVersionUID = -3423238140987292651L;
    private JPanel contentPane;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    MainFrame frame = new MainFrame();
                    frame.setVisible(true);
                }
                catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public MainFrame() {
        setTitle("Bingoo Handy");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        GridBagLayout gbl_contentPane = new GridBagLayout();
        gbl_contentPane.columnWidths = new int[] { 0, 0, 0 };
        gbl_contentPane.rowHeights = new int[] { 0, 0, 0, 0, 0, 0 };
        gbl_contentPane.columnWeights = new double[] { 0.0, 0.0, Double.MIN_VALUE };
        gbl_contentPane.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE };
        contentPane.setLayout(gbl_contentPane);

        JButton btnTranscoder = new JButton("Transcoder");
        btnTranscoder.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                showForm(new TranscoderFrame());
            }
        });
        GridBagConstraints gbc_btnTranscoder = new GridBagConstraints();
        gbc_btnTranscoder.fill = GridBagConstraints.HORIZONTAL;
        gbc_btnTranscoder.insets = new Insets(0, 0, 5, 5);
        gbc_btnTranscoder.gridx = 0;
        gbc_btnTranscoder.gridy = 0;
        contentPane.add(btnTranscoder, gbc_btnTranscoder);

        JButton btnUglifyJs = new JButton("Uglify JS");
        btnUglifyJs.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                showForm(new UglifyJsFrame());
            }
        });

        JButton btnSshShell = new JButton("SSH Shell");
        btnSshShell.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                showForm(new SshFrame());
            }
        });
        GridBagConstraints gbc_btnSshShell = new GridBagConstraints();
        gbc_btnSshShell.fill = GridBagConstraints.HORIZONTAL;
        gbc_btnSshShell.insets = new Insets(0, 0, 5, 0);
        gbc_btnSshShell.gridx = 1;
        gbc_btnSshShell.gridy = 0;
        contentPane.add(btnSshShell, gbc_btnSshShell);
        GridBagConstraints gbc_btnUglifyJs = new GridBagConstraints();
        gbc_btnUglifyJs.fill = GridBagConstraints.HORIZONTAL;
        gbc_btnUglifyJs.insets = new Insets(0, 0, 5, 5);
        gbc_btnUglifyJs.gridx = 0;
        gbc_btnUglifyJs.gridy = 1;
        contentPane.add(btnUglifyJs, gbc_btnUglifyJs);

        JButton btnIbatisLogSql = new JButton("iBatis Log SQL");
        btnIbatisLogSql.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                showForm(new IbatisLogSqlFrame());
            }
        });
        GridBagConstraints gbc_btnIbatisLogSql = new GridBagConstraints();
        gbc_btnIbatisLogSql.insets = new Insets(0, 0, 5, 5);
        gbc_btnIbatisLogSql.fill = GridBagConstraints.HORIZONTAL;
        gbc_btnIbatisLogSql.gridx = 0;
        gbc_btnIbatisLogSql.gridy = 2;
        contentPane.add(btnIbatisLogSql, gbc_btnIbatisLogSql);

        JButton btnMavenDeployer = new JButton("Maven Deployer");
        btnMavenDeployer.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                MavenDeployer.show();
            }
        });

        JButton btnSqlLeakSuspects = new JButton("SQL Leak Suspects");
        btnSqlLeakSuspects.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                showForm(new SqlLeakSuspectsFrame());
            }
        });
        GridBagConstraints gbc_btnSqlLeakSuspects = new GridBagConstraints();
        gbc_btnSqlLeakSuspects.fill = GridBagConstraints.HORIZONTAL;
        gbc_btnSqlLeakSuspects.insets = new Insets(0, 0, 5, 0);
        gbc_btnSqlLeakSuspects.gridx = 1;
        gbc_btnSqlLeakSuspects.gridy = 2;
        contentPane.add(btnSqlLeakSuspects, gbc_btnSqlLeakSuspects);
        GridBagConstraints gbc_btnMavenDeployer = new GridBagConstraints();
        gbc_btnMavenDeployer.insets = new Insets(0, 0, 5, 5);
        gbc_btnMavenDeployer.gridx = 0;
        gbc_btnMavenDeployer.gridy = 3;
        contentPane.add(btnMavenDeployer, gbc_btnMavenDeployer);

        JButton btnXml = new JButton("XML");
        btnXml.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                showForm(new XmlFrame());
            }
        });
        GridBagConstraints gbc_btnXml = new GridBagConstraints();
        gbc_btnXml.insets = new Insets(0, 0, 0, 5);
        gbc_btnXml.fill = GridBagConstraints.HORIZONTAL;
        gbc_btnXml.gridx = 0;
        gbc_btnXml.gridy = 4;
        contentPane.add(btnXml, gbc_btnXml);
    }

    private void showForm(final JFrame frame) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                    frame.addWindowListener(new WindowAdapter() {
                        @Override
                        public void windowClosing(WindowEvent winEvt) {
                            try {
                                Swings.savePerferences(frame);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    });
                    Swings.loadPreferences(frame);

                    frame.setVisible(true);
                }
                catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

}
